<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="记录日期" prop="recordDate">
      <el-input v-model="dataForm.recordDate" placeholder="记录日期" disabled></el-input>
    </el-form-item>
    <el-form-item label="气温" prop="temperature">
      <el-input v-model="dataForm.temperature" placeholder="气温"></el-input>
    </el-form-item>
    <el-form-item label="降水量" prop="precipitation">
      <el-input v-model="dataForm.precipitation" placeholder="降水量"></el-input>
    </el-form-item>
    <el-form-item label="风速" prop="windSpeed">
      <el-input v-model="dataForm.windSpeed" placeholder="风速（m/s）"></el-input>
    </el-form-item>
    <el-form-item label="风速等级" prop="windSpeedLevel">
      <el-input v-model="dataForm.windSpeedLevel" placeholder="风速等级"></el-input>
    </el-form-item>
    <el-form-item label="高温天数" prop="gwDays">
      <el-input v-model="dataForm.gwDays" placeholder="高温天数(一个月)"></el-input>
    </el-form-item>
    <el-form-item label="灾害天数" prop="disasterDays">
      <el-input v-model="dataForm.disasterDays" placeholder="灾害天数（类似高温天数的处理方式）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          recordDate: '',
          temperature: '',
          precipitation: '',
          windSpeed: '',
          windSpeedLevel: '',
          gwDays: '',
          disasterDays: ''
        },
        dataRule: {
          temperature: [
            { required: true, message: '气温不能为空', trigger: 'blur' }
          ],
          precipitation: [
            { required: true, message: '降水量不能为空', trigger: 'blur' }
          ],
          windSpeed: [
            { required: true, message: '风速通常以米 / 秒（m/s）等单位表示，可能存在小数部分。例如，风速为 3.5m/s 可以直接存储为 3.5。不能为空', trigger: 'blur' }
          ],
          windSpeedLevel: [
            { required: true, message: '风速等级不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/mh/weather/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.recordDate = data.weather.recordDate
                this.dataForm.temperature = data.weather.temperature
                this.dataForm.precipitation = data.weather.precipitation
                this.dataForm.windSpeed = data.weather.windSpeed
                this.dataForm.windSpeedLevel = data.weather.windSpeedLevel
                this.dataForm.gwDays = data.weather.gwDays
                this.dataForm.disasterDays = data.weather.disasterDays
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mh/weather/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'recordDate': this.dataForm.recordDate,
                'temperature': this.dataForm.temperature,
                'precipitation': this.dataForm.precipitation,
                'windSpeed': this.dataForm.windSpeed,
                'windSpeedLevel': this.dataForm.windSpeedLevel,
                'gwDays': this.dataForm.gwDays,
                'disasterDays': this.dataForm.disasterDays
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
